// pages/cart/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopCartList: [],
    checkAll: false,
    price: 0
  },
  changeItem(e) {
    console.log(e);
    let {
      index
    } = e.currentTarget.dataset
    this.data.shopCartList[index].status = !this.data.shopCartList[index].status
    let res = this.data.shopCartList.every(i => i.status == true)
    this.setData({
      checkAll: res
    })
    this.getPrice()
    this.keepData()
  },
  checkedAll() {
    this.setData({
      checkAll: !this.data.checkAll
    })
    this.data.shopCartList.forEach(item => item.status = this.data.checkAll)
    this.getPrice()
    this.keepData()
  },
  keepData() {
    this.setData({
      shopCartList: this.data.shopCartList
    })
    //重新赋值
    wx.setStorageSync('shopCar', this.data.shopCartList)
    wx.setStorageSync('checkAll', this.data.checkAll)
  },
  onChange(e) {
    console.log(e);
    let { index} = e.currentTarget.dataset
    let detail = e.detail
    this.data.shopCartList[index].num = detail
    this.keepData()
    this.getPrice()
  },
  getPrice() {
    let priceAll = 0
    this.data.shopCartList.forEach(item => {
      if (item.status == true) {
        priceAll += item.num * item.goods_price
      }
    })
    this.setData({
      price: priceAll
    })
    this.keepData()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      shopCartList: wx.getStorageSync('shopCar') || [],
      checkAll: wx.getStorageSync('checkAll') || false
    })
    this.getPrice()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})